﻿var manager = null;

$(function () {
    $("#departmentType").ligerComboBox({
        cancelable: false,
        width: 120,
        valueFieldID: 'hdnDepartmentType'
    });

    $('#btnSearch').click(function () {
        search();
    });

    $('#btnAdd').click(function () {
        edit();
    });

    $('#btnSearch').click();
});

//加载列表
function initTable(queryData, pageNumber) {
    $("#divMaingrid").html('<div id="maingrid"></div>');

    manager = $("#maingrid").ligerGrid({
        url: '/Sysbase/Department/Search?ram=' + Math.random(),
        parms: queryData,
        page: pageNumber,
        columns: [
            {
                display: '部门类型', name: 'DepartmentType', width: 100, render: function (row) {
                    var html = "";
                    if (row.DepartmentType == "00") {
                        html = "护理部";
                    }
                    else if (row.DepartmentType == "01") {
                        html = "医务部";
                    }
                    else
                    {
                        html = "其他";
                    }
                    return html;
                }
            },
            { display: '部门名称', name: 'DepartmentName', align: 'left', width: 200 },
            { display: '部门编码', name: 'DepartmentCode', align: 'left', width: 150 },
            {
                display: '状态', name: 'Status', width: 150, render: function (row) {
                    var html = "";
                    if (row.Status == 0) {
                        html = "正常";
                    }
                    else if (row.Status == 1) {
                        html = "停用";
                    }
                    else {
                        html = "其他";
                    }
                    return html;
                }
            },
            {
                display: '操作', name: '', minWidth: 80, render: function (row) {
                    return '<div style="heigth:100%;width:80px;text-align: left;margin:0px auto;"><a href="javascript:void(0);" class="a-edit" onclick="edit(' + row.Id + ')">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:void(0);" class="a-del" onclick="del(' + row.Id + ')">删除</a></div>';
                }
            }
        ],
        usePager: false,
        rownumbers: true,
        tree: {
            columnName: 'DepartmentName',
            idField: 'DepartmentCode',
            parentIDField: 'ParentId'
        },
        height: '100%',
        heightDiff: 25
    });
    GridNoData($("#maingrid"),true);
}

//搜索
function search(pageNumber) {
    var queryData = {
        departmentName: $("#txtDepartmentName").val(),
        departmentCode: $("#txtDepartmentCode").val(),
        departmentType: $("#hdnDepartmentType").val()
    }
    initTable(queryData, pageNumber);
}

//添加/编辑
function edit(id) {
    $.ligerDialog.open({
        height: 500,
        width: 430,
        title: '编辑部门',
        url: '/Sysbase/Department/Edit?id=' + id,
        showMax: false,
        showToggle: false,
        showMin: false,
        isResize: false,
        slide: false,
        data: {}
    });
}

//删除
function del(id) {
    $.ligerDialog.confirm('确定删除？', function (value) {
        if (value) {
            var url = "/Sysbase/Department/Delete";
            $.ajax({
                url: url,
                type: "POST",
                cache: false,
                data: { id: id },
                success: function (data) {
                    var d = $.parseJSON(data);
                    if (d.Result == 1) {
                        $.ligerDialog.success("删除成功");
                        $('#btnSearch').click();
                    }
                    else if (d.Result == 0) {
                        $.ligerDialog.error(d.Body);
                    }
                    else {
                        $.ligerDialog.error("程序错误");
                    }
                },
                error: function () {
                    $.ligerDialog.error("程序错误");
                }
            });
        }
    });
}